# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class JT_9206:
    result=[]
    Alarm_marker = ['紧急报警', '超速报警', '疲劳驾驶', '危险预警', 'GNSS模块发生故障', 'GNSS天线未接或被剪断', 'GNSS天线短路', '终端电源欠压',
                    '终端电源掉电', '终端LCD或显示器故障', 'TTS模块故障', '摄像头故障', '道路运输证IC卡模块故障', '超速预警', '疲劳驾驶预警',
                    '保留', '保留', '保留', '当天累计驾驶超时', '超时停车', '进出区域', '进出路线', '路段驾驶时间不足/过长', '路线偏离报警',
                    '车辆VSS故障', '车辆油量异常', '车辆被盗', '车辆非法点火', '车辆非法位移', '碰撞预警', '侧翻预警', '非法开门', '视频信号丢失',
                    '视频信号遮挡', '存储单元故障报警', '其他视频设备故障', '客车超员', '异常驾驶行为', '特殊报警录像达到存储阈值', '预留']
    alarm_data_str = ""
    analysisDescription=['服务器地址长度：','服务器地址：','端口：','用户名长度：','用户名：','密码长度：',
                         '密码：','文件上传路径长度：','文件上传路径：','逻辑通道号：','开始时间：','结束时间：','报警标志：'
                        ,'音视频资源类型：']
    analysis9206Length=[1,0,2,1,0,1,0,1,0,1,6,6,8,1]
    
    def LogicAisle(self, protocolStr, header):
        count=0

        for i in range(len(self.analysisDescription)):
            #服务器地址长度，用户名长度，密码长度，文件上传路径长度
            if i==0 or i==3 or i==5 or i==7:
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                print(s)
                str_length=int(s,16)
                self.analysis9206Length[i+1]=str_length
                for str_len in self.analysis9206Length:
                    print(str_len)
                header.append(self.analysisDescription[i] + s + "\n")
                count = count + self.analysis9206Length[i] * 2
            #服务器地址,用户名，密码，文件上传路径
            elif i==1 or i==4 or i==6 or i==8:
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)].decode("hex").decode('gb2312')
                print(s)
                header.append(self.analysisDescription[i] + str(s) + "\n")
                count = count + self.analysis9206Length[i] * 2
            #端口
            elif i==2:
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                print(s)
                s = str(int(s, 16))
                header.append(self.analysisDescription[i] + str(s) + "\n")
                count = count + self.analysis9206Length[i] * 2
            #逻辑通道号
            elif i==9:
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                header.append(self.analysisDescription[i] + s + "\n")
                count = count + self.analysis9206Length[i] * 2
            #开始时间/结束时间
            elif i==10 or i==11:
                c_time = 0
                times = []
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                for j in range(0, 6):
                    time = s[c_time:c_time + 2]
                    times.append(time)
                    c_time = c_time + 2
                header.append(
                    self.analysisDescription[i] + times[0] + "-" + times[1] + "-" + times[2] + "-" + times[3] + "-" +
                    times[4] + "-" + times[5] + "\n")
                count = count + self.analysis9206Length[i] * 2
            #报警标志
            elif i==12:
                # 十六进制字符串
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                # 转换为二进制字符串
                s = bin(int(s, 16))
                # 位数不足补零
                n = int(s[2:])
                n = "%064d" % n
                header.append(self.analysisDescription[i] + n + "\n")
                self.setalarmnumber(n, header)
                count = count + self.analysis9206Length[i] * 2
            #音视频资源类型
            elif i==13:
                s = protocolStr[count:count + (self.analysis9206Length[i] * 2)]
                if s == "00":
                    header.append(self.analysisDescription[i] + s + " 音视频" + "\n")
                elif s == "01":
                    header.append(self.analysisDescription[i] + s + " 音频" + "\n")
                elif s == "02":
                    header.append(self.analysisDescription[i] + s + " 视频" + "\n")
                elif s == "03":
                    header.append(self.analysisDescription[i] + s + " 视频或音频" + "\n")
                count = count + self.analysis9206Length[i] * 2

            self.result=header

    def setalarmnumber(self,s,header):
        count=64
        alarm_data_list=[]
        self.alarm_data_str=s
        for i in range(count):
            alarm_data=self.alarm_data_str[count-1:count]
            print(alarm_data)
            if alarm_data=="1":
                if i>38 or i==15 or i==16 or i==17:
                    pass
                else:
                    header.append(self.Alarm_marker[i]+'\n')
            alarm_data_list.append(alarm_data)
            count -=1


